<?php

use Illuminate\Database\Capsule\Manager as DB;

#[店铺商品类目]
class Dianpu_CategoryController extends AcommonController
{
    public function init()
    {
        parent::init();
        $this->table = 'category';
        $this->model = (new categoryModel())->newQueryWithoutScope('dianpu_id')->with(['dianpu'])->where('dianpu_id', '>', 0);
    }

    #[获取店铺类目]
    public function indexAction($returnData = false)
    {
        if ($this->isPost()) {
            $indexQuery = clone $this->model;
            $query = $indexQuery->where('pid', '=', 0);
            $page = $this->get('page', 1);
            $limit = $this->get('rows', 10);
            $offset = ($page - 1) * $limit;
            $sort = $this->get('sort', 'created_at');
            $order = $this->get('order', 'desc');
            $keywordsType = $this->get('keywords_type', 'dianpu_id');
            $keywords = trim($this->get('keywords', ''));
            if (!empty($keywords)) {
                switch ($keywordsType){
                    case 'dianpu_id':
                        $query = $query->where('dianpu_id', '=', $keywords);
                        break;
                    case 'member_id':
                        $query = $query->whereIn('dianpu_id',
                                                 \app\modules\Admin\models\merchantModel::query()->where('member_id', '=',$keywords)->pluck('dianpu_id', 'id')->toArray());
                        break;
                    case 'member_phone':
                        $query = $query->whereIn('dianpu_id',
                                                 \app\modules\Admin\models\merchantModel::query()
                                                                                        ->whereIn('member_id',
                                                                                                  memberModel::query()
                                                                                                             ->where('phone', 'like', "%{$keywords}%")
                                                                                                             ->where('status', 'normal')
                                                                                                             ->pluck('id')
                                                                                                             ->toArray()
                                                                                        )->pluck('dianpu_id', 'id')->toArray()
                        );
                        break;
                    default:
                }
            }
            $total = $query->count();
            $rows = $query->orderBy($sort, $order)
                          ->orderBy('id', 'ASC')
                          ->offset($offset)
                          ->limit($limit)
                          ->get();
            if ($rows->isNotEmpty()) {
                foreach ($rows as $item) {
                    $this->getChild($item);
                }
            }
            json(['total' => $total, 'rows' => $rows]);
        } else {
            $this->_view->display(strtolower($this->controller) . '/index.html');
        }
    }

    private function getChild(&$item)
    {
        $childrenQuery = clone $this->model;
        $children = $childrenQuery->where('pid', $item->id)->orderBy('weigh', 'DESC')->orderBy('id', 'ASC')->get();
        if ($children->isNotEmpty()) {
            $item->children = $children;
            foreach ($children as $v) {
                $v->name = htmlentities($v->name);
                $this->getChild($v);
            }
        }
    }

    #[目录树]
    private function catTree()
    {
        $catTreeQuery = clone $this->model;
        $items = $catTreeQuery->where('pid', 0)->select('id', 'name as text')->orderBy('weigh', 'DESC')->orderBy('id', 'ASC')->get();
        if ($items->isNotEmpty()) {
            foreach ($items as $item) {
                $this->getTreeChild($item);
            }
        }
        return $items;
    }

    private function getTreeChild(&$item)
    {
        $getTreeChildQuery = clone $this->model;
        $children = $getTreeChildQuery->where('pid', $item->id)->select('id', 'name as text')->orderBy('weigh', 'DESC')->orderBy('id', 'ASC')->get();
        if ($children->isNotEmpty()) {
            $item->children = $children;
            foreach ($children as $v) {
                $this->getTreeChild($v);
            }
        }
    }

}
